﻿/*

========================================================================

	                        Light Prepass Renderer

		                        by Floris Groen

                         E-mail : fmfgroen@yahoo.com
                     Blog : www.florisgroen.blogspot.com
		      

========================================================================

*/

using LightPrePassRenderer.Engine.Interfaces;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace LightPrePassRenderer.Engine
{
    public class CompositeMapping : EffectParameterMapping
    {
        EffectParameter world;
        EffectParameter view;
        EffectParameter projection;
        EffectParameter light;
        EffectParameter texture;
        EffectParameter materialDiffuse;
        EffectParameter specularIntensity;
        EffectParameter specular;
        EffectParameter halfPixel;

        IRenderManager renderManager;
        Renderer renderer;

        public CompositeMapping(Effect effect, GameServiceContainer services) : base(effect, services)
        {
            world = effect.Parameters["World"];
            view = effect.Parameters["View"];
            projection = effect.Parameters["Projection"];

            light = effect.Parameters["Light"];

            texture = effect.Parameters["Texture"];
            materialDiffuse = effect.Parameters["Diffuse"];
            specularIntensity = effect.Parameters["SpecularIntensity"];
            specular = effect.Parameters["Specular"];

            halfPixel = effect.Parameters["HalfPixel"];

            renderManager = (IRenderManager)services.GetService(typeof(IRenderManager));
            renderer = (Renderer)services.GetService(typeof(IRenderer));
        }

        public override void SetParametersPerFrame()
        {
            Matrix viewMatrix, projectionMatrix;

            renderManager.Camera.GetView(out viewMatrix);
            renderManager.Camera.GetProjection(out projectionMatrix);

            view.SetValue(viewMatrix);
            projection.SetValue(projectionMatrix);
            light.SetValue(renderer.Light.GetTexture());

            halfPixel.SetValue(renderer.HalfPixel);
        }

        public override void SetParametersPerObject(RenderEntry entry)
        {
            world.SetValue(entry.World);
            texture.SetValue(entry.Material.Texture);
            materialDiffuse.SetValue(entry.Material.DiffuseColor.ToVector3());
            specular.SetValue(entry.Material.SpecularColor.ToVector3());
            specularIntensity.SetValue(entry.Material.SpecularIntensity);

            Effect.CommitChanges();
        }
    }
}
